//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//

// Import the interfaces
#import "OptionScene.h"
#import "SimpleAudioEngine.h"
#import "MainScene.h"

extern BOOL isSound;
extern BOOL isMusic;
extern BOOL isNormal;

// HelloWorld implementation
@implementation OptionScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	OptionScene *layer = [OptionScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		BitmapFontAtlas *label1 = [BitmapFontAtlas bitmapFontAtlasWithString:@"Sound" fntFile:@"fort32.fnt"];
		MenuItemLabel *title1 = [MenuItemLabel itemWithLabel:label1 target:self selector:nil];
		[title1 setIsEnabled:NO];
		[MenuItemFont setFontName:@"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItemToggle *item1 = [MenuItemToggle itemWithTarget:self selector:@selector(SetSound:) items:
							   [MenuItemFont itemFromString:@"On"],
							   [MenuItemFont itemFromString: @"Off"],
							   nil];
		
		if (isSound == YES) {
			item1.selectedIndex = 0;
		}
		else {
			item1.selectedIndex = 1;
		}

		
		BitmapFontAtlas *label2 = [BitmapFontAtlas bitmapFontAtlasWithString:@"Music" fntFile:@"fort32.fnt"];
		MenuItemLabel *title2 = [MenuItemLabel itemWithLabel:label2 target:self selector:nil];
		[title2 setIsEnabled:NO];
		[MenuItemFont setFontName:@"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItemToggle *item2 = [MenuItemToggle itemWithTarget:self selector:@selector(SetMusic:) items:
							   [MenuItemFont itemFromString:@"On"],
							   [MenuItemFont itemFromString:@"Off"],
							   nil];
		if (isMusic == YES) {
			item2.selectedIndex = 0;
		}
		else {
			item2.selectedIndex = 1;
		}
		
		BitmapFontAtlas *label3 = [BitmapFontAtlas bitmapFontAtlasWithString:@"Difficulty" fntFile:@"fort32.fnt"];
		MenuItemLabel *title3 = [MenuItemLabel itemWithLabel:label3 target:self selector:nil];
		[title3 setIsEnabled:NO];
		[MenuItemFont setFontName:@"Marker Felt"];
		[MenuItemFont setFontSize:34];
		MenuItemToggle *item3 = [MenuItemToggle itemWithTarget:self selector:@selector(SetDifficulty:) items:
							   [MenuItemFont itemFromString:@"Normal"],
							   [MenuItemFont itemFromString:@"Hard"],
							   nil];
		if (isNormal == YES) {
			item3.selectedIndex = 0;
		}
		else {
			item3.selectedIndex = 1;
		}
		
		[MenuItemFont setFontName: @"Marker Felt"];
		[MenuItemFont setFontSize:34];
		
		BitmapFontAtlas *label = [BitmapFontAtlas bitmapFontAtlasWithString:@"Back" fntFile:@"fort32.fnt"];
		MenuItemLabel *back = [MenuItemLabel itemWithLabel:label target:self selector:@selector(GoBack:)];
		
		Menu *menu = [Menu menuWithItems:
					  title1, item1,
					  title2, item2,
					  title3, item3,
					  back, nil];
		[menu alignItemsInColumns:
		 [NSNumber numberWithUnsignedInt:2],
		 [NSNumber numberWithUnsignedInt:2],
		 [NSNumber numberWithUnsignedInt:2],
		 [NSNumber numberWithUnsignedInt:1],
		 nil];
		
		[self addChild:menu];
		// create and initialize a Label
//		[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
//		[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"mymemory.mp3" loop:YES];

	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

- (void) SetMusic: (id) sender
{
	if ([sender selectedIndex] == 0) {
		isMusic = YES;
		[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
	}
	else {
		isMusic = NO;
		[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
	}
}

- (void) SetSound: (id) sender
{
	if ([sender selectedIndex] == 0) {
		isSound = YES;
	}
	else {
		isSound = NO;
	}
}

- (void) SetDifficulty: (id) sender
{
	if ([sender selectedIndex] == 0) {
		isNormal = YES;
	}
	else {
		isNormal = NO;
	}
}

- (void) GoBack: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[MainScene node]]];
}

@end
